<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2019/1/24
 * Time: 14:26
 */

namespace app\admin\controller;


use app\model\EnclosureLogModel;
use app\model\EnclosureModel;
use app\model\EquipmentModel;
use think\Controller;
use think\Db;

class Statistics extends Common
{
    /**
     * @return \think\response\View
     * 运行统计页面
     */
   public function run_count(){
       return view('run_count');
   }

    /**
     * @return \think\response\View
     * 停留详情
     */
   public function stop_details(){
       return view('stop_details');
   }

    /**
     * @return \think\response\View
     * 报警统计
     */
   public function callpolice_count(){
       return view('callpolice_count');
   }

    /**
     * @return \think\response\View
     * 电子栅栏
     */
   public function electronic_fence(){
       $data = input('post.');
       $where2=[];
       $where=[];
       $whe=$this->sess_where();
       if ($whe){//普通用户

           $where2=['user_id'=>$whe];
       }
       if (isset($data['username'])) {
           if ($data['username'] != '') {//设备名
               $where['nickname'] = ['LIKE', '%' . $data['username'] . '%'];
           }
       }
       $user_id = Db::name('gps_app_user')->where($where)->column('id');
       $wh['user_id'] = ['in',$user_id];
       $data['data']=EnclosureModel::where($wh)->where($where2)->paginate(null,false,['query'=>request()->param()]);
       $user = Db::name('gps_app_user')->column('id,nickname');
       $this->assign('user',$user);
       return view('electronic_fence',$data);
   }
    /*
        * 导出电子栅栏
        */
    public function export_ele(){
        $data = input('get.');
        $where=[];
        $where2=[];
        $whe=$this->sess_where();
        if ($whe){//普通用户
            $where2=['user_id'=>$whe];
        }
        if (isset($data['username'])) {
            if ($data['username'] != '') {//设备名
                $where['user_name'] = ['LIKE', '%' . $data['username'] . '%'];
            }
        }
        $data =EnclosureModel::field('*')->alias('e')
            ->where($where2)
            ->order('e.id','desc')
            ->join('gps_app_user','gps_app_user.id = e.user_id')
            ->field('nickname')
            ->select();


        //表头
        $header = ['序号','用户','围栏名称','围栏半径','设置地址','状态'];
        //用于遍历的二维数组
        $result_data=array();
        $state=[1=>'已启用',2=>'禁用'];
        for($i=0;$i<count($data);$i++){
            $new = array();
            $new[] = $i+1;
            $new[] =  $data[$i]['nickname'];
            $new[] =  $data[$i]['en_name'];
            $new[] =  $data[$i]['radius'];
            $new[] =  $data[$i]['address'];
            $new[] = $state[$data[$i]['state']];
            array_push($result_data,$new);
        }
        //$filename 文件名
        $filename = "围栏".date("Y/m/d");
//        ExcelModel::getExcelFile(NULL,$header,$result_data,$filename,30);
        export($result_data, $header, $filename);
    }
    /**
     * @return \think\response\View
     * 报警详情
     */
    public function callpolice_details(){
        $data = input('post.');
        $where =[];
        $whe=$this->equs();
        if ($whe){
            $where=['imei'=>['in',$whe]];
        }
        if (isset($data['imei'])){
            if ($data['imei']!=''){//设备名
                $where['imei'] = ['LIKE', '%'.$data['imei'].'%'];
            }
            if ($data['start']!='' and $data['end'] ==''){//开始时间
                $where['addtime'] = ['>= time',strtotime($data['start'])];
            }elseif($data['start']=='' and $data['end'] !=''){//结束时间
                $where['addtime'] = ['<= time',strtotime($data['end'])+86399];
            }elseif($data['start']!='' and $data['end'] !=''){
                if ($data['start']>$data['end']){
                    $where['addtime']=['between time',[strtotime($data['end']),strtotime($data['start'])+86399]];
                }else{
                    $where['addtime']=['between time',[strtotime($data['start']),strtotime($data['end'])+86399]];
                }
            }
        }
        $where['code']= ['>',0];
        $data['data']=EnclosureLogModel::where($where)->order('addtime','desc')->paginate(null,false,['query'=>request()->param()]);
        return view('callpolice_details',$data);
    }
    /*
        * 导出报警详情
        */
    public function export_cal(){
        $data = input('get.');
        $where =[];
        $whe=$this->equs();
        if ($whe){
            $where=['imei'=>['in',$whe]];
        }
        if (isset($data['imei'])){
            if ($data['imei']!=''){//设备名
                $where['imei'] = ['LIKE', '%'.$data['imei'].'%'];
            }
            if ($data['start']!='' and $data['end'] ==''){//开始时间
                $where['addtime'] = ['>= time',strtotime($data['start'])];
            }elseif($data['start']=='' and $data['end'] !=''){//结束时间
                $where['addtime'] = ['<= time',strtotime($data['end'])+86399];
            }elseif($data['start']!='' and $data['end'] !=''){
                if ($data['start']>$data['end']){
                    $where['addtime']=['between time',[strtotime($data['end']),strtotime($data['start'])+86399]];
                }else{
                    $where['addtime']=['between time',[strtotime($data['start']),strtotime($data['end'])+86399]];
                }
            }
        }
        $where['code']= ['>',0];
        $data=EnclosureLogModel::where($where)->field('*')->select();

        //表头
        $header = ['序号','设备号（IMEI）','出/入','出入次数','位置','时间'];
        //用于遍历的二维数组
        $result_data=array();
        for($i=0;$i<count($data);$i++){
            $new = array();
            $new[] = $i+1;
            $new[] =  'IMEI：'.$data[$i]['imei'];
            $new[] =  $data[$i]['data'];
            $new[] =  $data[$i]['num'];
            $new[] =  $data[$i]['place'];
            $new[] = date('Y年m月d日H:i:s',$data[$i]['addtime']);
            array_push($result_data,$new);
        }
        //$filename 文件名
        $filename = "出入栅栏记录".date("Y/m/d");
//        ExcelModel::getExcelFile(NULL,$header,$result_data,$filename,30);
        export($result_data, $header, $filename);
    }


}